<template>
  <div>
    <el-form ref="form" label-width="80px">
      <el-form-item label="SPU名称">{{spu.spuName}}</el-form-item>
      <el-form-item label="SKU名称">
        <el-input placeholder="sku名称" v-model="skuInfo.skuName"></el-input>
      </el-form-item>
      <el-form-item label="价格(元)">
        <el-input placeholder="价格(元)" type="number" v-model="skuInfo.price"></el-input>
      </el-form-item>
      <el-form-item label="重量(千克)">
        <el-input placeholder="重量(千克)" v-model="skuInfo.weight"></el-input>
      </el-form-item>
      <el-form-item label="规格描述">
        <el-input type="textarea" rows="4" v-model="skuInfo.skuDesc"></el-input>
      </el-form-item>
      <el-form-item label="平台属性">
        <el-form :inline="true" label-width="80px">
          <el-form-item :label="attr.attrName" v-for="(attr,index) in attrInfoList" :key="attr.id">
            <el-select placeholder="请选择" v-model="attr.attrIdAndValue">
              <el-option :label="attrValue.valueName" v-for="(attrValue,i) in attr.attrValueList" :key="attrValue.id" :value="`${attr.id}:${attrValue.id}`"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <el-form-item label="销售属性">
        <el-form :inline="true" label-width="80px">
          <el-form-item :label="saleAttr.saleAttrName" v-for="(saleAttr,index) in spuSaleAttrList" :key="saleAttr.id">
            <el-select placeholder="请选择" v-model="saleAttr.attrIdAndValue">
              <el-option :label="saleAttrValue.saleAttrValueName" v-for="(saleAttrValue,index) in saleAttr.spuSaleAttrValueList" :key="saleAttrValue.id" :value="`${saleAttr.id}:${saleAttrValue.id}`"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <el-form-item label="图片列表">
        <el-table style="width:100%" border :data="spuImgList" @selection-change="handleSelectionChange">
          <el-table-column prop="prop" label="label" width="80px" type="selection" align="center">

          </el-table-column>
          <el-table-column prop="prop" label="图片" width="width">
            <template slot-scope="{row,$index}">
              <img :src="row.imgUrl" alt="" style="width:100px;height:100px">
            </template>
          </el-table-column>
          <el-table-column prop="imgName" label="名称" width="width">

          </el-table-column>
          <el-table-column prop="prop" label="操作" width="width">
            <template slot-scope="{row,$index}">
              <el-button type="primary" v-if="row.isDefault===0" @click="changeDefault(row)">设置默认</el-button>
              <el-button v-else>默认</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="save">保存</el-button>
        <el-button @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      spuImgList: [],
      spuSaleAttrList: [],
      attrInfoList: [],
      skuInfo: {
        category3Id: 0,
        price: 0,
        spuId: 0,
        tmId: 0,
        weight: '',
        skuName: '',
        skuDefaultImg: '',
        // 描述
        skuDesc: '',
        // sku属性值列表
        skuAttrValueList: [
          // {
          //   attrId: 0,
          //   attrName: 'string',
          //   id: 0,
          //   skuId: 0,
          //   valueId: 0,
          //   valueName: 'string'
          // }
        ],

        skuImageList: [
          /*   {
            id: 0,
            imgName: 'string',
            imgUrl: 'string',
            isDefault: 'string',
            skuId: 0,
            spuImgId: 0
          } */
        ],
        // sku销售属性列表
        skuSaleAttrValueList: [
          /* {
            id: 0,
            saleAttrId: 0,
            saleAttrName: 'string',
            saleAttrValueId: 0,
            saleAttrValueName: 'string',
            skuId: 0,
            spuId: 0
          } */
        ]
      },
      spu: {},
      // 手机table每一行的图片数据
      imgList: []
    }
  },
  methods: {
    // 获取sku数据初始化
    async getData(category1Id, category2Id, row) {
      // 接收已有的父组件数据
      this.skuInfo.category3Id = row.category3Id
      this.skuInfo.spuId = row.id
      this.skuInfo.tmId = row.tmId
      this.spu = row
      //  获取图片
      let spuImgListResult = await this.$API.spu.reqSpuImageList(row.id)
      if (spuImgListResult.code === 200) {
        let list = spuImgListResult.data
        list.forEach(item => {
          item.isDefault = 0
        })
        this.spuImgList = list
      }
      // 获取销售属性
      let spuSaleAttrListResult = await this.$API.spu.reqSpuSaleAttrList(row.id)
      if (spuSaleAttrListResult.code === 200) {
        this.spuSaleAttrList = spuSaleAttrListResult.data
      }
      let attrInfoListResult = await this.$API.spu.reqAttrInfoList(category1Id, category2Id, row.category3Id)
      if (attrInfoListResult.code === 200) {
        this.attrInfoList = attrInfoListResult.data
      }
    },
    // table表格复选框按钮事件
    handleSelectionChange(params) {
      // params获取用户选中的图片信息 但缺少字段isDefault用于控制显示默认按钮的属性
      this.imgList = params
    },
    // 默认按钮事件回调
    changeDefault(row) {
      this.spuImgList.forEach(item => {
        item.isDefault = 0
      })
      row.isDefault = 1
      this.skuInfo.skuDefaultImg = row.imgUrl
    },
    cancel() {
      this.$emit('changeScene_sku', 0)
      Object.assign(this._data, this.$options.data())
    },
    save() {
      // 整理参数 并提交

      const { attrInfoList, skuInfo, spuSaleAttrList, imgList } = this
      // 整理参数方式1：
      /*   let arr = []
      attrInfoList.forEach(item => {
        if (item.attrIdAndValue) {
          const [attrId, valueId] = item.attrIdAndValue.split(':')
          let obj = {
            attrId,
            valueId
          }
          arr.push(obj)
        }
      })
      skuInfo.skuSaleAttrValueList = arr */
      // 平台属性
      skuInfo.skuAttrValueList = attrInfoList.reduce((prev, item) => {
        if (item.attrIdAndValue) {
          const [attrId, valueId] = item.attrIdAndValue.split(':')
          prev.push({ attrId, valueId })
        }
        // 每次需要返回prev当做下一次循环时的prev
        return prev
      }, [])
      // 销售属性
      skuInfo.skuSaleAttrValueList = spuSaleAttrList.reduce((prev, item) => {
        if (item.attrIdAndValue) {
          const [saleAttrId, saleAttrValueId] = item.attrIdAndValue
          prev.push({
            saleAttrId,
            saleAttrValueId
          })
        }
        return prev
      }, [])
      skuInfo.skuImageList = imgList.map(item => ({
        imgName: item.imgName,
        imgUrl: item.imgUrl,
        isDefault: item.isDefault,
        spuImgId: item.spuImgId
      }))
      this.$API.spu.reqAddSku(skuInfo).then(
        res => {
          console.log(res)
          if (res.code === 200) return this.$message({ type: 'success', message: '添加sku成功' })
         
        },
        err => {
          this.$message({ type: 'error', message: '添加sku失败' })
        }
      )
    }
  }
}
</script>

<style>
</style>
